package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseFragment;
import com.wgts.wgts.control.eventbus.PayEvenBus;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerInvitationRecordFragmentComponent;
import com.wgts.wgts.dagger.module.InvitationRecordFragmentModule;
import com.wgts.wgts.mvp.presenter.IinvitationRecordFragmentPresenter;
import com.wgts.wgts.mvp.view.IinvitationRecordFragmentView;
import com.wgts.wgts.netmodle.resp.RecordSpreadEntity;
import com.wgts.wgts.netmodle.resp.RecordSummaryEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.CountDownTime;
import com.wgts.wgts.tools.CountDownTimerListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class InvitationRecordFragment extends BaseFragment implements IinvitationRecordFragmentView {
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    Unbinder unbinder;

    private static final String TYPE = "type";
    @BindView(R.id.img_bg)
    ImageView imgBg;

    private CountDownTimerListener countDownTimerListener;

    private int time = 5;


    @Inject
    IinvitationRecordFragmentPresenter presenter;


    @Override
    public void init() {

        DaggerInvitationRecordFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .invitationRecordFragmentModule(new InvitationRecordFragmentModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.invitation_record_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        CountDownTime.getInstance().removeCountDownTimerListener(countDownTimerListener);
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    private void initView() {
        CustomLineaLayoutManager customLineaLayoutManager = new CustomLineaLayoutManager(getContext());
        recycleView.setLayoutManager(customLineaLayoutManager);

        if (getArguments().getInt(TYPE) == 0) {
            if (AppInfor.getInstance().getUserState() < 2) {
                imgBg.setImageResource(R.mipmap.vip_back_bottom);
                presenter.initSpread("VIP");
            } else {
                imgBg.setImageResource(R.mipmap.supervip_back_bottom);
                presenter.initSpread("SVIP");
            }
        } else {
            if (AppInfor.getInstance().getUserState() < 2) {
                presenter.initSummary("VIP");
            } else {
                presenter.initSpread("SVIP");
            }
        }

    }

    public static Fragment getInstance(int type) {
        Fragment fragment = new InvitationRecordFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onError(int status, String msg) {

    }

    @Override
    public void initSpreadResult(List<RecordSpreadEntity> recordSpreadEntity) {

        final RvAdapterRecordSpreadAdapter adapter = new RvAdapterRecordSpreadAdapter(getContext(), recordSpreadEntity);
        recycleView.setAdapter(adapter);

        if (recordSpreadEntity.size() > 10) {

            countDownTimerListener = new CountDownTimerListener() {
                @Override
                public void countDownTimerListener() {
                    time--;
                    if (time == 0) {
                        time = 5;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.updateList();
                            }
                        });
                    }
                }
            };

            CountDownTime.getInstance().addCountDownTimerListener(countDownTimerListener);
        }
    }

    @Override
    public void initSummaryResult(RecordSummaryEntity recordSummaryEntity) {

        final RvAdapterRecordSummaryAdapter adapter = new RvAdapterRecordSummaryAdapter(getContext(), recordSummaryEntity);
        recycleView.setAdapter(adapter);

        if (recordSummaryEntity.list.size() > 10) {

            countDownTimerListener = new CountDownTimerListener() {
                @Override
                public void countDownTimerListener() {
                    time--;
                    if (time == 0) {
                        time = 5;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.updateList();
                            }
                        });
                    }
                }
            };

            CountDownTime.getInstance().addCountDownTimerListener(countDownTimerListener);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(PayEvenBus event) {
        if (getArguments().getInt(TYPE) == 0) {
            if (AppInfor.getInstance().getUserState() < 2) {
                imgBg.setImageResource(R.mipmap.vip_back_bottom);
                presenter.initSpread("VIP");
            } else {
                imgBg.setImageResource(R.mipmap.supervip_back_bottom);
                presenter.initSpread("SVIP");
            }
        } else {
            if (AppInfor.getInstance().getUserState() < 2) {
                presenter.initSummary("VIP");
            } else {
                presenter.initSpread("SVIP");
            }
        }
    }

    ;
}
